package com.atguigu.gmall.task.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 李旭
 * @create: 2021-07-26 09:09
 *
 *   死信队列的Java配置
 *
 *   spring.xml
 *   <bean class=""
 *
 **/
@Configuration
public class DeadLetterConfig {


    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    //交换机
    @Bean
    public Exchange deadExchange(){
        return ExchangeBuilder.directExchange(exchange_dead).build();
    }
    //死信队列
    @Bean
    public Queue deadQueue(){
        Map<String, Object> arguments = new HashMap<>();
        //过期转发的交换机 死信交换机是谁
        arguments.put("x-dead-letter-exchange",exchange_dead);
        arguments.put("x-dead-letter-routing-key",routing_dead_2);
        arguments.put("x-message-ttl", 10 * 1000);//10S
        return QueueBuilder.durable(queue_dead_1).withArguments(arguments).build();
    }
    //普通列队
    @Bean
    public Queue queue(){
        return QueueBuilder.durable(queue_dead_2).build();
    }
    //绑定
    @Bean
    public Binding deadExchangeBingingDeadQueue(){
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(routing_dead_1).noargs();
    }
    @Bean
    public Binding deadExchangeBingingQueue(){
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(routing_dead_2).noargs();
    }
}
